Ext.define('PJ.view.quotamanagement.List', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.quotamanagementList'],
    requires: ['AM.default.DefaultSearchForm',
               'AM.default.DateTime', 
               'AM.default.DefaultDateField',
               'AM.default.DefaultDoubleDateField',
               'AM.default.jsonp.DefaultLoadJsonp',//jsonp加载数据
		       'AM.default.DefaultActiveStateCombo',
		       'PJ.view.quotamanagement.AdjustTheQuotaList',//调整名额
		       'PJ.view.quotamanagement.PlanPlacesList'//计划名额
               ],
    initComponent: function(){
    	var me= this;
    	this.columns = [{
    		header: '培训班编号',
    		dataIndex: 'classNum',
    		width: 100,
    		renderer: function(value, meta, record){
    			meta.style = "white-space:normal;cursor: pointer;color:blue;";
    			meta.tdAttr = "data-qtip='" + value + "' classId=" + record.get("classId");
    			return value;
    		},
    		listeners: {
    			click: function(g, clm){
    				var classId = clm.getAttribute("classId");
    				win = Ext.create('PJ.view.trainingmanagement.Edit', {
    					title: "查看培训班",
    					flag:4,
    					grid: me,
    					isDisable:false,
    					isReadOnly:true,
    					classId:classId,
    					isHideExpectTime: false
    				}).show();
    				var actionName = "load";
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					actionName = "loadJsonp";
    				}
    				win.down("form").getForm().doAction(actionName,{
    					url:me.store.preUpdateUrl,
    					params:{
    						classId: classId
    					},
    					success:function(f,action){
    						var data = action.response.responseText;
    	        			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    	        			var form = win.down('form');	 
    	        			var baseForm = form.getForm();
    	        			baseForm.findField("classNum").setValue(json.data.classNum);
    	        			baseForm.findField("classNum_").setValue(json.data.classNum);
    					}
    				});

    			}
    		}
    	},{
            header: '培训班名称',
            dataIndex: 'className',
            flex: 1,
            minWidth: 110,
            renderer: function(value, meta, record){
            	meta.tdAttr = "data-qtip='" + value + "'";
            	return value;
            }
        },{
            header: '承办部委',
            dataIndex: 'ministrieName',
            width: 100,
            renderer: function(value, meta, record){
            	meta.tdAttr = "data-qtip='" + value + "'";
            	return value;
            }
        },{
            header: '开班时间',
            dataIndex: 'beginDate',
            width: 90 
        },{
            header: '结业时间',
            dataIndex: 'endDate',
            width: 90 
        },{
            header: '已报人数/计划人数',
            dataIndex: 'reportedNum',
            width: 115,
            sortable: false,
            renderer: function(value, meta, record){
            	//meta.style = record.get("depUserNum") == 0 ? "" : "white-space:normal;cursor: pointer;";
            	//meta.tdAttr = "classID='" + record.get("classId") + "'reportedNum='" + record.get("depUserNum") + "'";
            	var reportNum = record.get("reportedNum");
            	var valueStr = record.get("classUserNum")=="-1"? '不限' : value;
				return reportNum + "/" + valueStr;
			}
        },{
            header: '审核状态',
            dataIndex: 'auditState',
            renderer: function(v, meta, record){
                if (v == 2) {
                	meta.tdAttr = "data-qtip='" + record.get("auditReason") + "'";
                    return '<span style="color:red;">' + '审核未通过' + '</span>';
                }
                else if(v == 1) {
                    return '<span style="color:green;">' + '审核通过' + '</span>';
                }else if(v == 3){
                	return '未审核';
                }
            },
            width: 80
        },{
            header: '班级状态',
            dataIndex: 'openState',
            renderer: function(v){
                if (v == 2) {
                    return '<span style="color:red;">' + '未开班' + '</span>';
                }
                else if(v == 1) {
                    return '<span style="color:green;">' + '已开班' + '</span>';
                }else if(v == 3){
                    return '<span style="color:red;">' + '已结班' + '</span>';
                }
            },
            width: 80
        },{
            header: '发布状态',
            dataIndex: 'publishState',
            renderer: function(v){
                if (v == 2) {
                    return '<span style="color:red;">' + '未发布' + '</span>';
                }
                else if(v == 1) {
                    return '<span style="color:green;">' + '已发布' + '</span>';
                }
            },
            width: 80
        },{
            header: '报名状态',
            dataIndex: 'signUpState',
            renderer: function(v){
                if (v == 2) {
                    return '<span style="color:red;">' + '未开始' + '</span>';
                }
                else if(v == 1) {
                    return '<span style="color:green;">' + '正在报名' + '</span>';
                }else if(v == 3) {
                    return '<span style="color:green;">' + '报名结束' + '</span>';
                }
            },
            width: 80
        }];
    	
        this.store = Ext.create('PJ.'+ prototype +'.quotamanagement.List',{
        	isLoad: true
        });
        this.callParent(arguments);
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
    },
    /**
     * 计划名额
     */
    onPlanplacesClick: function(){
		var me = this;
		var record = this.getSelectionModel().getSelection();
        if(this.getSelectionModel().getCount() > 1){
            Ext.Msg.show({
                title: '提示',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }
		Ext.create('AM.default.DefaultWindowView', {
            title: record[0].get("className"),
            height:550,
            width: 450,
            grid: me,
            classId:record[0].get("classId"),
            items: [{
                xtype: 'planPlacesList'
            }],
            listeners: {
            	close: function(){
            		me.getStore().load();
            	}
            }
        });
	},
	//导入名额
    onImportquotasClick: function(){
		var me = this;
    	Ext.create("PJ.view.quotamanagement.BatchImportEdit", {
			title: "导入名额",
			height: 110,
			width: 350,
			grid: me
		}).show();
    },
    /**
     * 调整名额
     */
    onAdjustTheQuotaClick: function(){
		var me = this;
		var record = this.getSelectionModel().getSelection();
        if(this.getSelectionModel().getCount() > 1){
            Ext.Msg.show({
                title: '提示',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }
		Ext.create('AM.default.DefaultWindowView', {
            title: "调整名额",
            height:550,
            width: 650,
            grid: me,
            classId:record[0].get("classId"),
            items: [{
                xtype: 'adjustTheQuotaList'
                    }],
            buttons: [{
                text: '确定',
                scope: this,
                handler: function(button){
                	button.up('window').down('adjustTheQuotaList').sub();
                }
            },{
                text: '关闭',
                scope: this,
                handler: function(button){
                	button.up('window').close();
                }
            }],
            listeners: {
            	close: function(){
            		me.getStore().load();
            	}
            }
        });
	},
    //判断按钮是否可以操作
    ifSelect: function(exportUrl, classId){
    	var records = this.getSelectionModel().getSelection();
    	var me = this;
        var store = me.getStore();
        var params=store.proxy.extraParams;
        var href= exportUrl;
        if(classId.length!=0){
        	 href= exportUrl + "?searchClassIds="+classId[0];
        	for(var i=1;i<classId.length;i++){
        		href += "&searchClassIds="+classId[i]
        	}
        }
        for(var param in params){
        	if(params[param] === undefined){
        		href+="&"+param+"=";
        	}else{
        		href+="&"+param+"="+params[param];
        	}
        }
        href = encodeURI(href);
        var atag = Ext.get("exportResume");
        atag.set({
            "href": href
        });
        atag.dom.click();
    },
    //模版下载
    onDownloadClick: function(){
    	var records = this.getSelectionModel().getSelection();
    	var me = this;
        var store = me.getStore();
        var atag = Ext.get("exportResume");
        atag.set({
            "href": store.downloadUrl
        });
        atag.dom.click();
    
    },
    //名额对比表
    onComparisonsClick: function(){
    	var me = this;
    	var record = me.getSelectionModel().getSelection();
    	var classId = new Array;
    	for(var i=0;i<record.length;i++){
    		classId.push(record[i].get("classId"));
    	}
    	me.ifSelect(me.store.exportTrainingUrl,classId);
    },
    //预览
    listeners: {
		selectionchange: function(selModel, selected){
			this.down('#' + this.oldId + "Planplaces").setDisabled(selected.length === 0);//计划名额
//			this.down('#' + this.oldId + "Importquotas").setDisabled(selected.length === 0);//导入名额
			this.down('#' + this.oldId + "AdjustTheQuota").setDisabled(selected.length === 0);//调整名额
//			this.down('#' + this.oldId + "Comparisons").setDisabled(selected.length === 0);//名额对比表
//			this.down('#' + this.oldId + "Download").setDisabled(selected.length === 0);//下载
		},
		viewready: function(){
			var me = this;
			//名额对比表按钮集合
        	var exportMenu = Ext.create('Ext.menu.Menu', {
                items: [{
                    text: '名额对比表',
                    iconCls: 'comparisonsIco',
                    listeners: {
                        click: function(btn){
                        	var record = me.getSelectionModel().getSelection();
                        	var classId = new Array;
                        	for(var i=0;i<record.length;i++){
                        		classId.push(record[i].get("classId"));
                        	}
                        	me.ifSelect(me.store.exportTrainingUrl,classId);
                    	}
                    }
                }, {
                    text: '计划名额表',
                    iconCls: 'planPlacesTableIco',
                    listeners: {
                    	click: function(btn){
                    		var record = me.getSelectionModel().getSelection();
                    		var classId = new Array;
                        	for(var i=0;i<record.length;i++){
                        		classId.push(record[i].get("classId"));
                        	}
                        	me.ifSelect(me.store.exportCourseUrl,classId);
                    	}
                    }
                }]
            });
			this.addDocked([{
				dock: 'top',
				xtype: "searchform",
				columnCount: 3,
				fieldDefaults: {
					labelAlign: 'right',
					labelWidth: 100
				},
				items: [{
					xtype: "textfield",
					fieldLabel: '培训班编号',
					name: "searchTrainingNum",
					maxLength: "50",
					emptyText: "请输入培训班编号"
				},{
					xtype: "textfield",
					fieldLabel: '培训班名称',
					name: "searchClassName",
					maxLength: "50",
					emptyText: "请输入培训班名称"
				},{
					xtype: "textfield",
					fieldLabel: '承办部委',
					name: "searchMinistrieName",
					maxLength: "50",
					emptyText: "请输入承办部委"
				}, {
    				xtype: 'defaultDate',
    				fieldLabel: '开班时间',
    				name: 'searchBeginDate',
    				id: 'searchBankQUSD',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'searchBankQUED',
    					isStart: true
    				},
    				emptyText: "请输入开班时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '结业时间',
    				labelSeparator: '',
    				name: 'searchEndDate',
    				id: 'searchBankQUED',
    				vtype: 'startLessEndDate',
    				vtypeText: '结束时间不能早于开始时间！',
    				startLessEndDate: {
    					targetCmpId: 'searchBankQUSD',
    					isStart: false
    				},
    				emptyText: "请输入结业时间"
    			},{
					xtype: "combo",
					fieldLabel: '审核状态',
					name: "searchAuditState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '审核通过'],[2,'审核未通过'],[3,'未审核']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				},{
					xtype: "combo",
					fieldLabel: '班级状态',
					name: "searchOpenState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '已开班'],[2,'已结班']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				},{
					xtype: "combo",
					fieldLabel: '发布状态',
					name: "searchPublishState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '已发布'],[2,'未发布']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				},{
					xtype: "combo",
					fieldLabel: '报名状态',
					name: "searchReportState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '已报名'],[2,'未报名']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				}],
				onResetClick: function(button){
					var form = button.up('form');
					var gridPanel = form.grid;
					var searchCategoryIndex = gridPanel.searchCategoryIndex;
					var newParams = {
							searchCateId: searchCategoryIndex
					};
					gridPanel.getStore().proxy.extraParams = {};

					// 4.0强制更改store参数写法
					var params = gridPanel.getStore().proxy.extraParams;
					Ext.apply(params, newParams);

					form.getForm().reset();
				},
				grid: this
			},{
				xtype: 'toolbar',
				dock: 'top',
				displayInfo: true,
				items: [{
					text: '导入名额',
					iconCls: 'batchImportRegistrationIco',
					itemId: this.oldId + "Importquotas",
					disabled: false,
					scope: this,
					handler: this.onImportquotasClick
				}, "-",{
					text: '导入模版下载',
					iconCls: 'templateIco',
					itemId: this.oldId + "Download",
					disabled: false,
					scope: this,
					handler: this.onDownloadClick
				},"-", {
					text: '计划名额',
					iconCls: 'planplacesIco',
					itemId: this.oldId + "Planplaces",
					disabled: true,
					scope: this,
					handler: this.onPlanplacesClick
				}, "-",{
                    text: '调整名额',
                    iconCls: 'adjustTheQuotaIco',
                    itemId: this.oldId + "AdjustTheQuota",
                    disabled: true,
                    scope: this,
                    handler: this.onAdjustTheQuotaClick
                }, "-",{
                    text: '导出名额对比表',
                    iconCls: 'comparisonsIco',
                    itemId: this.oldId + "Comparisons",
                    disabled: false,
                    scope: this,
                    handler: this.onComparisonsClick
                }/*,"-",{
                    text: '名额对比表',
                	xtype: 'button',
                    cls: 'floater',
                    textAlign: 'right',
                    arrowAlign: 'right',
                    itemId: this.oldId + 'Export',
                    enableToggle: true,
                    iconAlign: 'left',
                    iconCls: 'deriveIco',
                    pressedCls: 'x-btn-focus',
                    menuActiveCls: 'x-btn-menu-active',
                    menu: exportMenu,
                    disabled: false
				}*/,"->", this.countSelectPanel]
			}]);
		}
    }
});
